﻿using MediatR;
using Newtonsoft.Json;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.InventoryListCommand;
using static WMSpublic.ParseAPI;
using WMSpublic;
using System.ComponentModel.DataAnnotations;

namespace Warehouse.Read.API.Applcation.CommandHandlers.InventoryListCommandHandlers
{
    public class InventoryListSelectCommandHandlers : IRequestHandler<InventoryListSelectCommand, List<InventoryListSelectDto>>
    {
        public IWarehouseRepository warehouse;
        public IStorehouseRepository storehouse;

        public InventoryListSelectCommandHandlers(IWarehouseRepository warehouse, IStorehouseRepository storehouse)
        {
            this.warehouse = warehouse;
            this.storehouse = storehouse;
        }

        public async Task<List<InventoryListSelectDto>> Handle(InventoryListSelectCommand request, CancellationToken cancellationToken)
        {
            List<Products> product = await GetProductsInfo(1, 1000);
            List<ProductType> type = await GetproductType(1, 1000);
            var list = from a in product
                       join b in type on a.ProductTypeID equals b.ProductTypeID
                       join c in await warehouse.GetAllAsyns() on a.WarehouseId equals c.WarehouseId
                       join d in await storehouse.GetAllAsyns() on a.StorehouseId equals d.StorehouseId
                       select new InventoryListSelectDto
                       {
                           ProductsCode = a.ProductsCode,
                           ProductsName = a.ProductsName,
                           Specs = a.Specs,
                           ProductTypeID = a.ProductTypeID,
                           ProductTypeName = b.ProductTypeName,
                           WarehouseName = c.WarehouseName,
                           StorehouseName = d.StorehouseName,
                       };
            if (request.ProductsCode!= null)
            {
                list=list.Where(x => x.ProductsCode.Contains(request.ProductsCode));
            }
            if (request.ProductsName != null)
            {
                list = list.Where(x => x.ProductsName.Contains(request.ProductsName));
            }
            return list.ToList();
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductsInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<Products>>(postRep);
            return vendors.data.list;
        }
        /// <summary>
        /// 获取产品类别
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetproductType(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProduct?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<ProductType>>(postRep);
            return vendors.data.list;
        }
        /// <summary>
        /// 产品类别
        /// </summary>
        public class ProductType : Audit
        {
            /// <summary>
            /// 产品类别id
            /// </summary>
            [Key]
            public int ProductTypeID { get; set; }
            /// <summary>
            /// 类别编号
            /// </summary>
            public string? ProductTypeCode { get; set; }
            /// <summary>
            /// 类别名称
            /// </summary>
            public string? ProductTypeName { get; set; }
            /// <summary>
            /// 上级类别
            /// </summary>
            public int? ParentId { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? Premark { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreateTime { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
        /// <summary>
        /// 产品管理
        /// </summary>
        public class Products : Audit
        {
            /// <summary>
            /// 产品id
            /// </summary>
            [Key]
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
    }
}
